package controller;

import com.google.gson.Gson;
import entity.*;
import service.CartService;
import service.OrderService;
import service.impl.AddressServiceImpl;
import service.impl.CartServiceImpl;
import service.impl.OrderServiceImpl;
import util.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/7 19:23
 */
@WebServlet("/order")
public class OrderController extends BaseController {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 订单预览（未提交）---->参数响应到order.jsp
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String preView(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //获取参数
        String uid = request.getParameter("uid");
        //根据uid调用service--->获取地址和购物车数据
        //地址
        AddressServiceImpl addressService = new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));

        //购物车
        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));

        //响应数据
        request.setAttribute("addressList",addressList);
        request.setAttribute("cartList",cartList);

        return Constants.FORWARD + "/order.jsp";
    }
    /**
     * 订单创建
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String create(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {

        //获取参数
        String uid = request.getParameter("uid");
        String aid = request.getParameter("aid");
        String sum = request.getParameter("sum");


        //根据uid调用service--->获取地址和购物车数据
        orderService.createOrder(Integer.parseInt(uid),Integer.parseInt(aid),sum);


        //转发到订单展示页面

        return Constants.FORWARD + "/order?method=show";
    }

    /**
     * 订单展示（已提交）---->参数响应到orderList.jsp
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //判断登录
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        if (loginUser == null) {
            session.setAttribute("msg", "请先登录您的账号！");
            return Constants.REDIRECT + "/login.jsp";
        }


        //调用service
        List<Orders> ordersList = orderService.findOrdersByUid(loginUser.getUid());

        //响应前端
        request.setAttribute("orderList", ordersList);

        return Constants.FORWARD + "/orderList.jsp";
    }
    /**
     * 订单详情
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String detail(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {

        //获取参数
        String oid = request.getParameter("oid");

        //业务逻辑
        Orders order = orderService.findOrdersByOid(oid);

        //响应前端---单个订单详情
        request.setAttribute("order", order);

        return Constants.FORWARD + "/orderDetail.jsp";
    }
    /**
     * 支付成功
     * @param request
     * @param response
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public String success(HttpServletRequest request, HttpServletResponse response) {

        //获取参数
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");

        //业务逻辑
        Gson gson = new Gson();
        WeiXin weiXin = gson.fromJson(result, WeiXin.class);
        //得到支付结果
        String result_code = weiXin.getResult().getResult_code();
        if(result_code!=null && "SUCCESS".equals(result_code)){
            //支付成功
            //修改订单状态
            orderService.updateOrderStateByOid(oid);
            User loginUser = (User) request.getSession().getAttribute("loginUser");
            return Constants.FORWARD+"orders?method=showOrderList&uid="+loginUser.getUid();
        }else{
            //支付失败
            request.setAttribute("msg","订单"+oid+"支付失败!!");
            return Constants.FORWARD+"message.jsp";
        }
    }





}
